/*
 *格式
 */
export const formatModel = (thisObj, sourceObj) => {
  for (var key in thisObj) {
    if (sourceObj && typeof sourceObj[key] != "undefined") {
      if (thisObj[key] && Object.prototype.toString.call(thisObj[key]) === "[object Object]") {
        formatModel(thisObj[key], sourceObj[key]);
      } else {
        thisObj[key] = sourceObj[key];
      }
    }
  }
  return thisObj;
};

/* 判断是否为空对象 */
export const isEmptyObject = (obj) => {
  return Object.keys(obj).length === 0 && obj.constructor === Object;
};

/* 深拷贝对象 */
export const deepClone = (obj, hash = new WeakMap()) => {
  // 基本类型直接返回
  if (obj === null || typeof obj !== "object") {
    return obj;
  }

  // 日期对象
  if (obj instanceof Date) {
    return new Date(obj);
  }

  // RegExp 对象
  if (obj instanceof RegExp) {
    return new RegExp(obj);
  }

  // 数组
  if (Array.isArray(obj)) {
    return obj.map((item) => deepClone(item, hash));
  }

  // 对象
  if (obj.constructor === Object) {
    // 检查循环引用
    if (hash.has(obj)) {
      return hash.get(obj);
    }

    let cloneObj = {};
    hash.set(obj, cloneObj);

    for (let key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        cloneObj[key] = deepClone(obj[key], hash);
      }
    }

    return cloneObj;
  }

  // 对于其他对象，如函数、Map、Set等，这里简单处理为null或抛出错误
  // 实际应用中可以根据需求进行扩展
  throw new Error("Unable to copy obj! Its type is not supported.");
};

// 数组去除指定元素
export const delComArr = (arr, item) => {
  let needarr = [];
  const index = JSON.stringify(arr).indexOf(JSON.stringify(item));
  if (index > -1) {
    let newarr = JSON.stringify(arr).replace(JSON.stringify(item), "null");
    //console.log(newarr);
    needarr = JSON.parse(newarr);
    if (needarr.includes(null)) {
      let newindex = needarr.indexOf(null);
      if (newindex > -1) {
        needarr.splice(newindex, 1);
      }
    }
  } else {
    console.log("数组中不包含该对象");
  }
  return needarr;
};

// 生成随机文件名
export const fileName = (length) => {
  const data = [
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z",
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z"
  ];
  let nums = "";
  for (let i = 0; i < length; i++) {
    const r = parseInt(Math.random() * 61, 10);
    nums += data[r];
  }
  return nums;
};

//主题
//HEX格式转RGB格式
const hex2Rgb = (color) => {
  color = color.replace("#", "");
  const result = color.match(/../g);
  for (let i = 0; i < 3; i++) {
    result[i] = parseInt(result[i], 16);
  }
  return result;
};

//RGB格式转HEX格式
const rgb2Hex = (r, g, b) => {
  const hexs = [r.toString(16), g.toString(16), b.toString(16)];
  for (let i = 0; i < 3; i++) {
    if (hexs[i].length === 1) {
      hexs[i] = "0" + hexs[i];
    }
  }
  const result = "#" + hexs.join("");
  return result;
};

//使颜色变亮
export const lighten = (color, level) => {
  const rgb = hex2Rgb(color);
  for (let i = 0; i < 3; i++) {
    rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]);
  }
  const result = rgb2Hex(rgb[0], rgb[1], rgb[2]);
  return result;
};
// 使颜色变暗
export const darken = (color, level) => {
  const rgb = hex2Rgb(color);
  for (let i = 0; i < 3; i++) {
    rgb[i] = Math.floor(rgb[i] * (1 - level));
  }
  const result = rgb2Hex(rgb[0], rgb[1], rgb[2]);
  return result;
};

/**
 * 去除对象中值为空的键
 * @param {Object} obj - 要处理的对象
 * @param {Array} [emptyValues=[null, undefined, ""]] - 自定义被认为是空值的数组
 * @returns {Object} 处理后的新对象
 */
export const removeEmptyKeys = (obj, emptyValues = [null, undefined, ""]) => {
  // 如果不是对象或者是null（因为typeof null === 'object'），直接返回
  if (typeof obj !== "object" || obj === null) {
    return obj;
  }

  // 创建一个新对象，避免修改原对象
  const newObj = Array.isArray(obj) ? [] : {};

  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      const value = obj[key];

      // 递归处理嵌套对象
      if (typeof value === "object" && value !== null) {
        const nestedResult = removeEmptyKeys(value, emptyValues);
        // 如果处理后的嵌套对象不是空的，则添加到新对象中
        if (Object.keys(nestedResult).length > 0) {
          newObj[key] = nestedResult;
        }
      }
      // 如果值不在空值列表中，则保留
      else if (!emptyValues.includes(value)) {
        newObj[key] = value;
      }
    }
  }

  return newObj;
};
